package com.next.muscu.data;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "exo", catalog = "jaydb", schema = "")
public class Exo implements Comparable<Exo>,MyDataObject{
	

	public static final String PP_ID = "id";
	public static final String PP_EXOTYPE = "exoType";
	public static final String PP_Code = "Code";
	public static final String PP_DESCRIPTION = "description";
	public static final String PP_PDF_FILENAME = "PDFFileName";
	
        @ManyToOne(optional=false) 
        @JoinColumn(name="ID_EXOTYPE", nullable=false, updatable=false)
	private ExoType exoType = null;
	@Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
	@Column(name = "ID")
        private Integer id;
        @Column(name="CODE")
	private String Code = new String();
	@Column(name="DESCRIPTION")
        private String description = new String();
	@Column(name = "DATE_PHOTO")
        private String pdfFileName = new String();
	@Transient
        private PropertyChangeSupport ppChangeSupport = new PropertyChangeSupport(this); 
	
	public Exo()
	{
		
	}
	
	public Exo(int id,ExoType exoType, String code, String description) {
		super();
		this.id = id;
		this.exoType = exoType;
		Code = code;
		this.description = description;
	}
	public Exo(int id,ExoType exoType, String code, String description,String PDFFileName) {
		super();
		this.id = id;
		this.exoType = exoType;
		Code = code;
		this.description = description;
		this.pdfFileName = PDFFileName;
	}
	/**
	 * @return
	 * @uml.property  name="id"
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 * @uml.property  name="id"
	 */
	public void setId(Integer id) {
		Integer old = this.id;
		this.id = id;
		ppChangeSupport.firePropertyChange(PP_ID, old, this.id );
	}

	/**
	 * @return
	 * @uml.property  name="exoType"
	 */
	public ExoType getExoType() {
		return exoType;
	}

	/**
	 * @param exoType
	 * @uml.property  name="exoType"
	 */
	public void setExoType(ExoType exoType) {
		ExoType old = this.exoType;
		this.exoType = exoType;
		ppChangeSupport.firePropertyChange(PP_EXOTYPE, old, this.exoType );
	}

	/**
	 * @return
	 * @uml.property  name="code"
	 */
	public String getCode() {
		return Code;
	}

	/**
	 * @param code
	 * @uml.property  name="code"
	 */
	public void setCode(String code) {
		String old = new String(this.Code);
		Code = code;
		ppChangeSupport.firePropertyChange(PP_Code, old, this.Code);
	}

	/**
	 * @return
	 * @uml.property  name="description"
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 * @uml.property  name="description"
	 */
	public void setDescription(String description) {
		String old = new String(this.description);
		this.description = description;
		ppChangeSupport.firePropertyChange(PP_DESCRIPTION, old, this.description );
		
	}

	public String getPdfFileName() {
		return pdfFileName;
	}

	public void setPdfFileName(String pDFFileName) {
		String old = this.pdfFileName;
		pdfFileName = pDFFileName;
		ppChangeSupport.firePropertyChange(PP_PDF_FILENAME, old, this.pdfFileName );
	}

	@Override
	public String toString() {
		return "Exo [id=" +id +"exoType=" + exoType.toString() + ", Code=" + Code + ", description="
				+ description + "]";
	}

	public int compareTo(Exo arg0) {
		Integer a = new Integer(id);
		Integer b = new Integer(arg0.getId());
		return a.compareTo(b);
	}
	
	@Override
	 public boolean equals(Object o) {
	        if (!(o instanceof Exo))
	            return false;
	        Exo e = (Exo)o;
	        return e.getId()==id;
	    }
	
	@Override
	    public int hashCode() {
	        return id;
	    }
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		ppChangeSupport.addPropertyChangeListener(listener);
	}
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		ppChangeSupport.addPropertyChangeListener(propertyName, listener);
	}
	public PropertyChangeListener[] getPropertyChangeListeners() {
		return ppChangeSupport.getPropertyChangeListeners();
	}
	public PropertyChangeListener[] getPropertyChangeListeners(
			String propertyName) {
		return ppChangeSupport.getPropertyChangeListeners(propertyName);
	}
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		ppChangeSupport.removePropertyChangeListener(listener);
	}
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		ppChangeSupport
				.removePropertyChangeListener(propertyName, listener);
	}

	public boolean copy(MyDataObject o) {
        if (!(o instanceof Exo))
            return false;
        Exo e = (Exo)o;
        this.Code = e.getCode();
        this.description = e.getDescription();
        this.id = e.getId();
        this.pdfFileName = e.getPdfFileName();
        if(this.exoType == null)
        	this.exoType = new ExoType();
        this.exoType.copy(e.getExoType());
		return true;
	}

	@Override
	public void Create() {
		Code = "ZZ";
		description = "nouveau";	
		exoType = new ExoType(1,"","");
	}
    
}
